﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Bortosky.Google.Visualization;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Reports.Lib.Repository.Transactions;
using Icoders.MarketTracker.Web.Lib.Security;
using Icoders.MarketTracker.Web.Lib.Util.Helper;
using log4net;
using Icoders.MarketTracker.Reports.Lib.Util;

namespace Icoders.MarketTracker.Web.Controllers
{
     [CustomAuthorize]
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private INewsRepository _newsRepository;
        private IDashboardReportService _dashboardReportService;
        
        public HomeController(INewsRepository newsRepository, IDashboardReportService dashboardReportService)
        {
            _newsRepository = newsRepository;
            _dashboardReportService = dashboardReportService;
            
        }

        

        public ActionResult Index()
        {
            
            ILog log = LogManager.GetLogger(this.GetType().FullName);
            log.Warn("sadi the great");
            return View(_newsRepository.GetAll());
        }
        public ActionResult ChangeClient(string client)
        {
            HttpCookie cookie = new HttpCookie("SelectedClient", client);
            cookie.Expires = DateTime.Now.AddDays(200);
            Response.SetCookie(cookie);
             Response.Redirect("~/");
            return null;
        }
        public ActionResult Details(int id)
        {
            News news = new News();
            news = _newsRepository.GetById(id);
            return View(news);
        }
        [HttpPost]
        public JsonResult GetPieChartData()
        {

            var data = _dashboardReportService.GetSalePerSkuDashBoard(Common.GetClientIdFromCookies());

            return Json(data);
        }


        [HttpPost]
        public JsonResult GetBarChartData()
        {


            var data = _dashboardReportService.GetSalePerFsrDashBoard(Common.GetClientIdFromCookies());

            return Json(data);
        }
        [HttpPost]
        public JsonResult GetLineChartData()
        {


            var data = _dashboardReportService.GetCallageDashBoard(Common.GetClientIdFromCookies());

            return Json(data);
        }
        [HttpPost]
        public JsonResult GetStackedChartData()
        {


            var data = _dashboardReportService.GetSalePerSkuPerPeriodDashBoard(Common.GetClientIdFromCookies());
            //var data =
            //    _dashboardReportService.GetSalePerSkuPerPeriodDashBoard().Pivot(m => m.Product, m => m.Period, m3 => m3.Sum(m => m.Quantity));

            return Json(data);
        }

       [HttpPost]
        public ActionResult GetStackedTestChartData()
        {

            ContentResult result = new ContentResult();
            result.ContentType = "application/text";
            result.ContentEncoding = Encoding.UTF8;
            result.Content = null;
            DataTable data = _dashboardReportService.GetSalePerSkuPerPeriodDashBoard(Common.GetClientIdFromCookies()).ToList().ToDataTable();
            DataTable data2 = PivotHelper.Pivot(data, "Period", "Product", "Quantity");
           
            result.Content = new GoogleDataTable(data2).GetJson();

            return result;

        }

       
    }
}
